package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.UacRole;
import com.csun.cmny.provider.model.domain.UacUser;
import com.csun.cmny.provider.model.dto.user.*;
import com.csun.cmny.provider.model.vo.MenuVo;
import com.csun.cmny.provider.model.vo.UserVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.UacRoleService;
import com.csun.cmny.provider.service.UacUserService;
import com.csun.cmny.provider.utils.Md5Util;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2018-11-20 17:35
 * @Description:
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UacUserCommonController extends BaseController {

    @Resource
    private UacUserService uacUserService;

    @Resource
    private UacRoleService uacRoleService;

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    /**
     * 根据userId查询用户详细信息（连表查询）.
     *
     * @return the wrapper
     */
    @PostMapping(value = "/queryUserInfo/{loginName}")
    @ApiOperation(httpMethod = "POST", value = "根据userId查询用户详细信息")
    public Wrapper<UserVo> queryUserInfo(@PathVariable String loginName) {
        logger.info("根据userId查询用户详细信息");
        UserVo userVo = new UserVo();
        UacUser uacUser = uacUserService.findByLoginName(loginName);
        uacUser = uacUserService.findUserInfoByUserId(uacUser.getId());
        List<UacRole> roleList = uacRoleService.findAllRoleInfoByUserId(uacUser.getId());
        List<MenuVo> authTree = uacRoleService.getOwnAuthTree(uacUser.getId());
        BeanUtils.copyProperties(uacUser, userVo);
        if (PublicUtil.isNotEmpty(roleList)) {
            userVo.setRoles(new HashSet<>(roleList));
        }
        userVo.setAuthTree(authTree);
        //return WrapMapper.ok(userVo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), userVo);
    }


    /**
     * 校验登录名唯一性.
     *
     * @param checkLoginNameDto the check login name dto
     *
     * @return the wrapper
     */
    @PostMapping(value = "/checkLoginName")
    @ApiOperation(httpMethod = "POST", value = "校验登录名唯一性")
    public Wrapper<Boolean> checkLoginName(@ApiParam(name = "loginName", value = "登录名") @RequestBody CheckLoginNameDto checkLoginNameDto) {
        logger.info("校验登录名唯一性 checkLoginNameDto={}", checkLoginNameDto);

        Long id = checkLoginNameDto.getUserId();
        String loginName = checkLoginNameDto.getLoginName();

        Example example = new Example(UacUser.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("loginName", loginName);
        if (id != null) {
            criteria.andNotEqualTo("id", id);
        }

        int result = uacUserService.selectCountByExample(example);
        //return WrapMapper.ok(result < 1);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result<1);
    }

    /**
     * 校验邮箱唯一性.
     *
     * @param checkEmailDto the check email dto
     *
     * @return the wrapper
     */
    @PostMapping(value = "/checkEmail")
    @ApiOperation(httpMethod = "POST", value = "校验登录名唯一性")
    public Wrapper<Boolean> checkEmail(@RequestBody CheckEmailDto checkEmailDto) {
        logger.info("校验邮箱唯一性 checkEmailDto={}", checkEmailDto);

        Long id = checkEmailDto.getUserId();
        String email = checkEmailDto.getEmail();

        Example example = new Example(UacUser.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("email", email);
        if (id != null) {
            criteria.andNotEqualTo("id", id);
        }

        int result = uacUserService.selectCountByExample(example);
        //return WrapMapper.ok(result < 1);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result<1);
    }

    /**
     * 校验真实姓名唯一性.
     *
     * @param checkUserNameDto the check user name dto
     *
     * @return the wrapper
     */
    @PostMapping(value = "/checkUserName")
    @ApiOperation(httpMethod = "POST", value = "校验真实姓名唯一性")
    public Wrapper<Boolean> checkUserName(@ApiParam(name = "checkUserNameDto", value = "校验真实姓名唯一性Dto") @RequestBody CheckUserNameDto checkUserNameDto) {
        logger.info(" 校验真实姓名唯一性 checkUserNameDto={}", checkUserNameDto);
        Long id = checkUserNameDto.getUserId();
        String name = checkUserNameDto.getUserName();

        Example example = new Example(UacUser.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userName", name);
        if (id != null) {
            criteria.andNotEqualTo("id", id);
        }

        int result = uacUserService.selectCountByExample(example);
        //return WrapMapper.ok(result < 1);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result<1);
    }

    /**
     * 校验用户电话号码唯一性.
     *
     * @param checkUserPhoneDto the check user phone dto
     *
     * @return the wrapper
     */
    @PostMapping(value = "/checkUserPhone")
    @ApiOperation(httpMethod = "POST", value = "校验用户电话号码唯一性")
    public Wrapper<Boolean> checkUserPhone(@ApiParam(name = "checkUserPhoneDto", value = "校验用户电话号码唯一性Dto") @RequestBody CheckUserPhoneDto checkUserPhoneDto) {
        logger.info(" 校验用户电话号码唯一性 checkUserPhoneDto={}", checkUserPhoneDto);
        Long id = checkUserPhoneDto.getUserId();
        String mobileNo = checkUserPhoneDto.getMobileNo();
        Example example = new Example(UacUser.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("mobileNo", mobileNo);

        if (id != null) {
            criteria.andNotEqualTo("id", id);
        }

        int result = uacUserService.selectCountByExample(example);
        //return WrapMapper.ok(result < 1);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result<1);
    }

    /**
     * 校验新密码是否与原始密码相同.
     *
     * @param checkNewPasswordDto 修改密码实体
     *
     * @return the wrapper
     */
    @PostMapping(value = "/checkNewPassword")
    @ApiOperation(httpMethod = "POST", value = "校验新密码是否与原始密码相同")
    public Wrapper<Boolean> checkNewPassword(@ApiParam(name = "checkNewPasswordDto", value = "校验新密码是否与原始密码相同Dto") @RequestBody CheckNewPasswordDto checkNewPasswordDto) {
        logger.info(" 校验新密码是否与原始密码相同 checkNewPasswordDto={}", checkNewPasswordDto);
        String loginName = checkNewPasswordDto.getLoginName();
        String newPassword = checkNewPasswordDto.getNewPassword();
        UacUser uacUser = new UacUser();
        uacUser.setLoginName(loginName);
        int result = 0;
        UacUser user = uacUserService.findByLoginName(loginName);
        if (user == null) {
            logger.error("找不到用户. loginName={}", loginName);
        } else {
            uacUser.setLoginPwd(Md5Util.encrypt(newPassword));
            result = uacUserService.selectCount(uacUser);
        }
        //return WrapMapper.ok(result < 1);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result<1);
    }

    /**
     * 修改用户邮箱
     *
     * @param email     the email
     * @param emailCode the email code
     *
     * @return the wrapper
     */
    //@LogAnnotation
    @PostMapping(value = "/modifyUserEmail/{email}/{emailCode}")
    @ApiOperation(httpMethod = "POST", value = "修改用户信息")
    public Wrapper<Integer> modifyUserEmail(@PathVariable String email, @PathVariable String emailCode) {
        logger.info(" 修改用户信息 email={}, emailCode={}", email, emailCode);
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        uacUserService.modifyUserEmail(email, emailCode, loginAuthDto);
        //return WrapMapper.ok();
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null));
    }

    /**
     * 获取已有权限树
     *
     * @return the auth tree by role id
     */
    @PostMapping(value = "/getOwnAuthTree")
    @ApiOperation(httpMethod = "POST", value = "获取权限树")
    public Wrapper<List<MenuVo>> getOwnAuthTree() {
        List<MenuVo> tree = uacRoleService.getOwnAuthTree(getLoginAuthDto().getUserId());
        //return WrapMapper.ok(tree);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), tree);
    }

    /**
     * 获取菜单树
     */
    @PostMapping(value = "/queryOwnMenuTree")
    public Wrapper<List<MenuVo>> queryOwnMenuTree() {

        List<MenuVo> tree = uacUserService.queryOwnMenuTree(getLoginAuthDto());

        //return WrapMapper.ok(tree);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), tree);
    }

    /**
     * 获取用户信息.
     *
     * @return the information
     */
    @PostMapping(value = "/getInformation")
    @ApiOperation(httpMethod = "POST", value = "获取用户信息")
    public Wrapper<UserInfoDto> getInformation() {
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        Long userId = loginAuthDto.getUserId();
        logger.info("queryUserInfo - 查询用户基本信息 userId={}", userId);
        UacUser uacUser = uacUserService.queryByUserId(userId);
        if (uacUser == null) {
            return WrapMapper.error("找不到当前用户");
        }
        UserInfoDto userInfoDto = new UserInfoDto();
        BeanUtils.copyProperties(uacUser, userInfoDto);
        userInfoDto.setUserId(userId + "");
        userInfoDto.setGroupId(loginAuthDto.getGroupId());
        userInfoDto.setGroupName(loginAuthDto.getGroupName());

        //return WrapMapper.ok(userInfoDto);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), userInfoDto);
    }

}
